import 'package:flutter/material.dart';

class RouterContext {
  static RouterContext instance = RouterContext._();

  final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();

  RouterContext._();
}

class ARouter {
  static ARouter instance = ARouter._();

  ARouter._();

  Future pagePush(String pageName, {Map<String, dynamic>? arguments}) {
    return Navigator.of(RouterContext.instance.navigatorKey.currentContext!)
        .pushNamed(
      pageName,
      arguments: arguments,
    );
  }

  pagePushReplacement(String pageName, {Map<String, dynamic>? arguments}) {
    Navigator.of(RouterContext.instance.navigatorKey.currentContext!)
        .pushReplacementNamed(
      pageName,
      arguments: arguments,
    );
  }

  pagePop({Object? arguments}) {
    Navigator.of(RouterContext.instance.navigatorKey.currentContext!).pop(
      arguments,
    );
  }

  pagePopToRoot() {
    Navigator.of(RouterContext.instance.navigatorKey.currentContext!)
        .popUntil((route) {
      return route.isFirst;
    });
  }

  pagePopToRootThenPush(String pageName, {Map<String, dynamic>? arguments}) {
    Navigator.of(RouterContext.instance.navigatorKey.currentContext!)
        .pushNamedAndRemoveUntil(pageName, (route) => route.isFirst,
            arguments: arguments);
  }

  logout() {
    Navigator.of(RouterContext.instance.navigatorKey.currentContext!)
        .pushNamedAndRemoveUntil('/login', (route) => false);
  }
}
